

package xib.core;

import java.io.IOException;
import java.io.InputStream;
import xib.parserTools.interfaces.ISpeechBubbleData;
import xib.parserTools.interfaces.ISpeechBubbleData.SpeechBubbleType;
import xib.utils.XibDataException;
import xib.utils.XibIOFileException;

/**
 * This class contains the information from a specific SpeechBubble.
 * @author xavierds
 */
public final class SpeechBubbleXib {

    /**
     * Private field with the value of the imageIdentifier.
    */
    private String imageIdentifier;

    /**
     * Private field with the text of the imageIdentifier.
     */
    private String text;

    /**
     * Private fields with the values of the location from the X and Y axis.
     */
    private int locationX, locationY;

    /**
     * Private fields with the values of the width and height.
     */
    private int width, height;

     /**
     * Private field with the value of the type of SpeechBubble.
     */
    private SpeechBubbleType speechBubbleType;

    /**
     * Private field with the information about the InputStream.
     */
    private InputStream stream;

    private FileHandler fh;

    /**
     * Default Constructor. It initializes this SpeechBubbleXib from the given
     * ISpeechBubbleData. Before the assignement of the fields it validates
     * the SpeechBubbleData.
     * @param data A ISpeechBubbleData with the information for this SpeechBubbleXib.
     * @throws XibDataException If the given ISpeechBubbleXib wasn't initialized
     * properly.
     */
    public SpeechBubbleXib(ISpeechBubbleData data){
        data.validate();
        imageIdentifier = data.getImageIdentifier();
        text = data.getText();
        speechBubbleType = data.getSpeechBubbleType();
        locationX = data.getLocationX();
        locationY = data.getLocationY();
        width = data.getWidth();
        height = data.getHeight();
    }

    /**
     * Returns the image Identifier of this SpeechBubble, if this SpeechBubble
     * is an SpeechBubbleType.DefaultLanguage the String is an empty String.
     * @return A String with the image identifier of the speechBubble.
     */
    public String getImageIdentifier(){
        return imageIdentifier;
    }

    /**
     * Returns the text of this SpeechBubble. The String could be an empty String.
     * @return A String with the text of the SpeechBubble.
     */
    public String getText(){
        return text;
    }

    /**
     * Returns the SpeechBubbleType of this SpeechBubble.
     * @return A SpeechBubbleType.
     */
    public SpeechBubbleType getSpeechBubbleType(){
        return speechBubbleType;
    }

    /**
     * Returns the location from the X axis of this SpeechBubble. This location
     * is a relative value from the panel.
     * @return An integer with the location from the X axis.
     */
    public int getLocationX(){
        return locationX;
    }

    /**
     * Returns the location from the Y axis of this SpeechBubble. This location
     * is a relative value from the panel.
     * @return An integer with the location from the Y axis.
     */
    public int getLocationY(){
        return locationY;
    }

    /**
     * Returns the width of this SpeechBubble.
     * @return An integer with the width of the SpeechBubble.
     */
    public int getWidth(){
        return width;
    }

    /**
     * Returns the height of this SpeechBubble.
     * @return An integer width the height of the SpeechBubble.
     */
    public int getHeight(){
        return height;
    }

    /**
     * Returns the inputStream of this SpeechBubble.
     * @return An InputStream.
     */
    public InputStream getInputStream(){
        InputStream aux = stream;
        setInputStream(fh);
        return aux;
    }

    /**
     * Sets the InputStream of this SpeechBubble.
     * @param stream
     */
    public void setInputStream(InputStream stream){
        this.stream = stream;
    }

    void setInputStream(FileHandler fh){
        this.fh = fh;
        try{
            stream =  fh.getEntry(imageIdentifier);
        }
        catch(IOException ex){
            throw new XibIOFileException("The speechBubbleXib " + imageIdentifier +
                    " wasn't load correctly", ex);
        }
    }

}
